package com.zhiche.lisa.lspm.service.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import com.zhiche.lisa.lspm.vo.common.LicenseTypeVo;
import com.zhiche.lisa.lspm.vo.common.ServiceTypeVO;
import com.zhiche.lisa.lspm.vo.common.TransportationTypeVO;
import com.zhiche.lisa.lspm.vo.fleet.FleetTypeVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

import static java.util.stream.Collectors.toList;

/**
 * Created by zhaoguixin on 2018/6/29.
 */
@Component
public class MdmUtil {

    @Value("${lisa.mdm.selectCardTypeByCodeUrl}")
    private String selectCardTypeByCodeUrl;

    @Value("${lisa.mdm.licenTypeBySubjectUrl}")
    private String licenTypeBySubjectUrl;

    @Value("${lisa.mdm.licenTypeByIdUrl}")
    private String licenTypeByIdUrl;

    //连接超时时间
    @Value("${lisa.socketTimeout}")
    private int socketTimeout;

    @Value("${lisa.mdm.getServiceTypeByCodeUrl}")
    private String getServiceTypeByCodeUrl;

    @Value("${lisa.mdm.getAllServiceTypeUrl}")
    private String getAllServiceTypeUrl;

    @Value("${lisa.mdm.selectCardTypeByIdUrl}")
    private String selectCardTypeByIdUrl;

    @Value("${lisa.mdm.getTransportTypeByIdUrl}")
    private String getTransportTypeByIdUrl;

    @Value("${lisa.mdm.getTransportTypeByCodeUrl}")
    private String getTransportTypeByCodeUrl;

    @Value("${lisa.mdm.getAllTransportTypeUrl}")
    private String getAllTransportTypeUrl;

    @Value("${lisa.mdm.getAllFleetTransportTypeUrl}")
    private String getAllFleetTransportTypeUrl;

    @Value("${lisa.mdm.getLicenseTypeByCodeUrl}")
    private String getLicenseTypeByCodeUrl;

    @Value("${lisa.mdmUrl}")
    private String mdmUrl;
    @Value("${lisa.mdm.getAllFleetTypeByTypeUr1}")
    private String getAllFleetTypeByTypeUr1;

    private List<TransportationTypeVO> transportationTypeVOList;

    /**
     * 查询发卡方类型
     *
     * @param cardCode
     * @return
     */
    public String getCardType(String cardCode) {
        String cardType = "";
        // 查询发卡方类型
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", ""));
        List<NameValuePair> params = Lists.newArrayList();
        params.add(new BasicNameValuePair("code", cardCode));
        String strReturn = HttpClientUtil.get(selectCardTypeByCodeUrl, headNamePairs, params, socketTimeout);
        if (StringUtils.isNotEmpty(strReturn)) {
            RestfulResponse<String> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<String>>() {
            });
            if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getCode())) {
                cardType = restfulResponse.getData();
            }
        }
        return cardType;
    }

    /**
     * 根据主体类型查询证照类型
     *
     * @param subjectType
     * @return
     */
    public List<LicenseTypeVo> listLicenseTypeBySubject(String subjectType) {
        List<LicenseTypeVo> listLiceseType = Lists.newArrayList();
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", ""));
        List<NameValuePair> params = Lists.newArrayList();
        params.add(new BasicNameValuePair("subjectType", subjectType));
        String strReturn = HttpClientUtil.get(licenTypeBySubjectUrl, headNamePairs, params, socketTimeout);
        if (StringUtils.isNotEmpty(strReturn)) {
            RestfulResponse<List<LicenseTypeVo>> restfulResponse = JSON.parseObject(strReturn, new
                    TypeReference<RestfulResponse<List<LicenseTypeVo>>>() {
                    });
            if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                listLiceseType = restfulResponse.getData();
            }
        }
        return listLiceseType;
    }

    /**
     * 根据类型ID获取证照类型
     *
     * @param typeId
     * @return
     */
    public LicenseTypeVo getLicenseTypeById(String typeId) {
        LicenseTypeVo licenseType = null;
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", ""));
        List<NameValuePair> params = Lists.newArrayList();
        params.add(new BasicNameValuePair("typeId", typeId));
        String strReturn = HttpClientUtil.get(licenTypeByIdUrl, headNamePairs, params, socketTimeout);
        if (StringUtils.isNotEmpty(strReturn)) {
            RestfulResponse<LicenseTypeVo> restfulResponse = JSON.parseObject(strReturn, new
                    TypeReference<RestfulResponse<LicenseTypeVo>>() {
                    });
            if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                licenseType = restfulResponse.getData();
            }
        }
        return licenseType;
    }

    /**
     * 调用mdm获取证照类型
     *
     * @param typeCode
     * @param subjectCode
     * @return
     */
    public LicenseTypeVo getLicenseTypeByCode(String typeCode, String subjectCode) {
        LicenseTypeVo licenseTypeVo = null;
        try {
            List<NameValuePair> headNamePairs = Lists.newArrayList();
            headNamePairs.add(new BasicNameValuePair("Authorization", ""));
            List<NameValuePair> params = Lists.newArrayList();
            params.add(new BasicNameValuePair("typeCode", typeCode));
            params.add(new BasicNameValuePair("subjectType", subjectCode));
            String strReturn = HttpClientUtil.get(getLicenseTypeByCodeUrl, headNamePairs, params, socketTimeout);
            if (!org.springframework.util.StringUtils.isEmpty(strReturn)) {
                RestfulResponse<LicenseTypeVo> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<LicenseTypeVo>>() {
                });
                if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                    licenseTypeVo = restfulResponse.getData();
                }
            }
        } catch (Exception ex) {
            throw new BaseException(ex.getMessage());
        }

        return licenseTypeVo;
    }

    /**
     * 根据code得到服务类型
     *
     * @param codes
     * @return
     */
    public String getServiceTypeByCodes(String codes) {
        String strServiceTypes = null;
        try {
            List<NameValuePair> headNamePairs = Lists.newArrayList();
            headNamePairs.add(new BasicNameValuePair("Authorization", ""));
            List<NameValuePair> params = Lists.newArrayList();
            params.add(new BasicNameValuePair("codes", codes));
            String strReturn = HttpClientUtil.get(getServiceTypeByCodeUrl, headNamePairs, params, socketTimeout);
            if (StringUtils.isNotEmpty(strReturn)) {
                RestfulResponse<List<ServiceTypeVO>> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<ServiceTypeVO>>>() {
                });
                if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                    List<ServiceTypeVO> listServiceType = restfulResponse.getData();
                    if (CollectionUtils.isNotEmpty(listServiceType)) {
                        strServiceTypes = String.join(";", listServiceType.stream().map(serviceType -> serviceType.getValue()).collect(toList()));
                    }
                }
            }
        } catch (Exception ex) {
            throw new BaseException(ex.getMessage());
        }
        return strServiceTypes;
    }


    /**
     * 获取全部服务类型
     *
     * @return
     */
    public List<ServiceTypeVO> getAllServiceTypeList() {
        try {
            List<NameValuePair> headNamePairs = Lists.newArrayList();
            headNamePairs.add(new BasicNameValuePair("Authorization", ""));
            String strReturn = HttpClientUtil.get(getAllServiceTypeUrl, headNamePairs, socketTimeout);
            if (StringUtils.isNotEmpty(strReturn)) {
                RestfulResponse<List<ServiceTypeVO>> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<ServiceTypeVO>>>() {
                });
                if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                    List<ServiceTypeVO> listServiceType = restfulResponse.getData();
                    if (CollectionUtils.isNotEmpty(listServiceType)) {
                        return listServiceType;
                    }
                }
            }
        } catch (Exception ex) {
            throw new BaseException(ex.getMessage());
        }
        return null;
    }

    /**
     * 根据id获取发卡方类型
     *
     * @param id
     * @return
     */
    public String getCardIssuerTypeNameById(Long id) {
        String issuerTypeName = null;
        try {
            List<NameValuePair> headNamePairs = Lists.newArrayList();
            headNamePairs.add(new BasicNameValuePair("Authorization", ""));
            List<NameValuePair> params = Lists.newArrayList();
            params.add(new BasicNameValuePair("id", id.toString()));
            String strReturn = HttpClientUtil.get(selectCardTypeByIdUrl, headNamePairs, params, socketTimeout);
            if (StringUtils.isNotEmpty(strReturn)) {
                RestfulResponse<String> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<String>>() {
                });
                if (Objects.nonNull(restfulResponse) && StringUtils.isNotEmpty(restfulResponse.getData())) {
                    issuerTypeName = restfulResponse.getData();
                }
            }
        } catch (Exception ex) {
            throw new BaseException(ex.getMessage());
        }
        return issuerTypeName;
    }

    /**
     * 获取所有板车运输工具类型
     *
     * @return
     */
    public List<TransportationTypeVO> getAllTransportType() {
        List<TransportationTypeVO> transportationType = null;
        try {
            List<NameValuePair> headNamePairs = Lists.newArrayList();
            headNamePairs.add(new BasicNameValuePair("Authorization", ""));
            List<NameValuePair> params = Lists.newArrayList();
            String strReturn = HttpClientUtil.get(getAllTransportTypeUrl, headNamePairs, params, socketTimeout);
            if (StringUtils.isNotEmpty(strReturn)) {
                RestfulResponse<List<TransportationTypeVO>> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<TransportationTypeVO>>>() {
                });
                if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                    List<TransportationTypeVO> listServiceType = restfulResponse.getData();
                    if (CollectionUtils.isNotEmpty(listServiceType)) {
                        return listServiceType;
                    }
                }
            }
        } catch (Exception ex) {
            throw new BaseException(ex.getMessage());
        }
        return transportationType;
    }

    /**
     * 根据id获取运输工具类型
     *
     * @param id
     * @return
     */
    public TransportationTypeVO getTransportTypeById(Long id) {
        TransportationTypeVO transportationType = null;
        try {
            List<NameValuePair> headNamePairs = Lists.newArrayList();
            headNamePairs.add(new BasicNameValuePair("Authorization", ""));
            List<NameValuePair> params = Lists.newArrayList();
            params.add(new BasicNameValuePair("transportTypeId", String.valueOf(id)));
            String strReturn = HttpClientUtil.get(getTransportTypeByIdUrl, headNamePairs, params, socketTimeout);
            if (StringUtils.isNotEmpty(strReturn)) {
                RestfulResponse<TransportationTypeVO> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<TransportationTypeVO>>() {
                });
                if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                    transportationType = restfulResponse.getData();
                }
            }
        } catch (Exception ex) {
            throw new BaseException(ex.getMessage());
        }
        return transportationType;
    }

    /**
     * 根据code获取运输工具类型
     *
     * @param code
     * @return
     */
    public TransportationTypeVO getTransportTypeByCode(String code) {
        TransportationTypeVO transportationType = null;
        try {
            List<NameValuePair> headNamePairs = Lists.newArrayList();
            headNamePairs.add(new BasicNameValuePair("Authorization", ""));
            List<NameValuePair> params = Lists.newArrayList();
            params.add(new BasicNameValuePair("transportTypeCode", code));
            String strReturn = HttpClientUtil.get(getTransportTypeByCodeUrl, headNamePairs, params, socketTimeout);
            if (StringUtils.isNotEmpty(strReturn)) {
                RestfulResponse<TransportationTypeVO> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<TransportationTypeVO>>() {
                });
                if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                    transportationType = restfulResponse.getData();
                }
            }
        } catch (Exception ex) {
            throw new BaseException(ex.getMessage());
        }
        return transportationType;
    }

    /**
     * 根据车队类型Id获取车队类型
     *
     * @return
     */
    public List<FleetTypeVO> getAllFleetTypeByType() {
        List<FleetTypeVO> listFleetType = Lists.newArrayList();
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", ""));
        List<NameValuePair> params = Lists.newArrayList();
//    params.add(new BasicNameValuePair("fleetTypeId",fleetTypeId));
        String strReturn = HttpClientUtil.get(getAllFleetTypeByTypeUr1, headNamePairs, params, socketTimeout);
        if (StringUtils.isNotEmpty(strReturn)) {
            RestfulResponse<List<FleetTypeVO>> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<FleetTypeVO>>>() {
            });
            if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                listFleetType = restfulResponse.getData();
            }
        }
        return listFleetType;
    }

    /**
     * 获取全部的车辆类型
     *
     * @return
     */
    public List<TransportationTypeVO> getAllFleetTransportType() {
        String method = "/inner/transportType/getAllFleetTransportType";
        List<TransportationTypeVO> transportationTypeVOList = null;
        try {
            String strReturn = HttpClientUtil.get(mdmUrl + method, null, null, socketTimeout);
            if (StringUtils.isNotEmpty(strReturn)) {
                RestfulResponse<List<TransportationTypeVO>> restfulResponse = JSON.parseObject(strReturn, new
                        TypeReference<RestfulResponse<List<TransportationTypeVO>>>() {
                        });
                if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                    transportationTypeVOList = restfulResponse.getData();
                }
            }
        } catch (Exception ex) {
            throw new BaseException(ex.getMessage());
        }
        return transportationTypeVOList;
    }

    /**
     * 根据运输工具类型ID获取类型实体
     *
     * @param typeId
     * @return
     */
    public TransportationTypeVO getTransporType(Long typeId) {
        if (Objects.isNull(typeId)) return null;

        //获取车辆类型列表
        if (org.springframework.util.CollectionUtils.isEmpty(transportationTypeVOList)) {
            transportationTypeVOList = getAllFleetTransportType();
        }

        //匹配类型
        for (TransportationTypeVO transporType : transportationTypeVOList) {
            if (transporType.getId().equals(typeId)) {
                return transporType;
            }
        }
        return null;
    }
}
